EXECNAME=httpd
MAKE=make
CC=gcc
ROOT=$(shell pwd)
BUILD=build
CONTROLLER=controller
SERVICE=service
DAO=dao
MODE=mode
LIB=lib

CFLAGS= -g -Wall -I. -I$(ROOT)/include -L. -L$(ROOT)/lib -Wl,-rpath=$(ROOT)/$(LIB) 

LDFLAGS=-lmariadbclient

export  MAKE CC ROOT BUILD LIB CFLAGS LDFLAGS CONTROLLER SERVICE DAO MODE


SRC=$(wildcard *.c)
OBJS=$(patsubst %.c, %.o, $(SRC))

ALL: PREBUILD LIBS MODE DAO SERVICE CONTROLLER TARGET

PREBUILD:
	@rm -rf $(ROOT)/$(BUILD)
	@mkdir $(ROOT)/$(BUILD)
	@cp $(ROOT)/Makefile.target $(ROOT)/$(BUILD)/Makefile
	@cp $(ROOT)/config.xml $(ROOT)/$(BUILD)/
	@cp $(ROOT)/mode.xml $(ROOT)/$(BUILD)/

LIBS:
	$(MAKE) -C $(ROOT)/$(LIB)

CONTROLLER:
	$(MAKE) -C $(ROOT)/$(CONTROLLER)

SERVICE:
	$(MAKE) -C $(ROOT)/$(SERVICE)

DAO:
	$(MAKE) -C $(ROOT)/$(DAO)

MODE:
	$(MAKE) -C $(ROOT)/$(MODE)

TARGET: $(EXECNAME)
$(EXECNAME):$(OBJS)
	cd $(ROOT)/$(BUILD)/ && $(MAKE) -C $(ROOT)/$(BUILD)

%.o:%.c
	$(CC) -c $(CFLAGS) $< $(LDFLAGS) -o $(ROOT)/$(BUILD)/$@

.PHONY:clean
clean:
	$(MAKE) -C $(ROOT)/$(LIB) clean
	$(MAKE) -C $(ROOT)/$(CONTROLLER) clean
	$(MAKE) -C $(ROOT)/$(SERVICE) clean
	$(MAKE) -C $(ROOT)/$(DAO) clean
	$(MAKE) -C $(ROOT)/$(MODE) clean
	rm -rf $(OBJS) $(EXECNAME) $(BUILD)
